package cn.edu.nwpu.warehousemanagement.service.impl;

import cn.edu.nwpu.warehousemanagement.dao.InventoryRecordRepository;
import cn.edu.nwpu.warehousemanagement.dao.ProductRepository;
import cn.edu.nwpu.warehousemanagement.dao.ProductSalesPredictionRepository;
import cn.edu.nwpu.warehousemanagement.pojo.ProductSalesPrediction;
import cn.edu.nwpu.warehousemanagement.pojo.Result;
import cn.edu.nwpu.warehousemanagement.pojo.dto.PredictVO;
import cn.edu.nwpu.warehousemanagement.pojo.dto.TodayDayStatsVO;
import cn.edu.nwpu.warehousemanagement.pojo.dto.WeeklyTrendVO;
import cn.edu.nwpu.warehousemanagement.service.PredictService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class PredictServiceImp implements PredictService {
    @Autowired
    private InventoryRecordRepository inventoryRecordRepository;

    @Autowired
    private ProductRepository productRepository;

    @Autowired
    private ProductSalesPredictionRepository productSalesPredictionRepository;

    @Override
    public Result<PredictVO> getPredictByProductId(String productIdStr) {
        try {
            Long productId = Long.parseLong(productIdStr);

            //region:获取今天的出入库数量及当前数量
            LocalDateTime todayStart = LocalDate.now().atStartOfDay();

            long inboundCount;
            long outboundCount;

            inboundCount = getInboundCount(productId, todayStart);
            outboundCount = getOutboundCount(productId, todayStart);

            long currentStock = productRepository.findByIdAndDeletedFalse(productId).get().getQuantity();

            TodayDayStatsVO todayDayStatsVO = new TodayDayStatsVO();
            todayDayStatsVO.setInbound(inboundCount);
            todayDayStatsVO.setOutbound(outboundCount);
            todayDayStatsVO.setCurrentStock(currentStock);
            //endregion

            //region:获取7天的历史数据趋势
            LocalDateTime sevenDaysAgo = todayStart.minusDays(7);

            List<String> dates = new ArrayList<>();
            List<Long> inbounds = new ArrayList<>();
            List<Long> outbounds = new ArrayList<>();

            LocalDateTime indexDay = sevenDaysAgo;

            for (long i = 0; i < 7; i++) {
                dates.add(indexDay.toString());
                inbounds.add(getInboundCount(productId,indexDay));
                outbounds.add(getOutboundCount(productId,indexDay));

                indexDay = indexDay.plusDays(1);
            }

            WeeklyTrendVO weeklyTrendVO = new WeeklyTrendVO(dates, inbounds, outbounds);

            //endregion

            PredictVO predictVO = new PredictVO();
            predictVO.setTodayDayStatsVO(todayDayStatsVO);
            predictVO.setWeeklyTrendVO(weeklyTrendVO);


            ProductSalesPrediction productSalesPrediction = productSalesPredictionRepository.findByProductId(productIdStr);


            predictVO.setPredictOutbound(productSalesPrediction.getAllData());

            return Result.success(predictVO);
        }catch (Exception e){
            return Result.error("查无此物或无预测信息");
        }
    }

    public long getInboundCount(Long productId, LocalDateTime localDateTime){
        LocalDateTime startDay = localDateTime;
        LocalDateTime endDay = localDateTime.plusDays(1);

        long inboundCount = inventoryRecordRepository.countTodayInboundByProductId(productId, startDay, endDay);

        return inboundCount;
    }

    public long getOutboundCount(Long productId, LocalDateTime localDateTime){
        LocalDateTime startDay = localDateTime;
        LocalDateTime endDay = localDateTime.plusDays(1);

        long outboundCount = inventoryRecordRepository.countTodayOutboundByProductId(productId, startDay, endDay);

        return outboundCount;
    }
}
